package com.douwong.student.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.douwong.student.R;
import com.douwong.student.utils.ViewUtils;
import com.douwong.student.view.LoadingPager;

/**
 * @autor yichao
 * @time 2017/6/1214:22
 * @des 数据加载fragment基类
 */

public abstract class BaseLoadingFragment extends BaseFragment {

    private Context mContext;
    private LoadingPager mLoadingPager;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }
    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        if (mLoadingPager == null) {
            mLoadingPager = new LoadingPager(mContext, R.layout.loadpage_loading, R.layout.loadpage_error, R.layout.loadpage_empty) {
                @Override
                protected View createSuccessView() {
                    return BaseLoadingFragment.this.createSuccessView();
                }

                @Override
                protected LoadResult load() {
                    return BaseLoadingFragment.this.load();
                }
            };
            mLoadingPager.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    mLoadingPager.show();
                }
            });
        } else {
            ViewUtils.removeSelfFromParent(mLoadingPager);// 移除frameLayout之前的爹
        }

        return mLoadingPager;
    }

    public void show() {
        if (mLoadingPager != null) {
            mLoadingPager.show();
        }
    }

    /**
     * 访问网络数据，返回
     */
    protected abstract LoadingPager.LoadResult load();

    /**
     * 加载数据成功View
     */
    protected abstract View createSuccessView();
}
